<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: inc.prediction_users.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
    /**
     * send mail to prediction users
     *
     * @param integer $project_id
     */
    function jl_pred_user_sendmail($project_id) {
      global $database, $mainframe;
        if ($project_id > 0) {
          $projecttip = new JoomLeague($database);
          $projecttip->load($project_id);
          $showUser_config=$projecttip->getTemplateConfig("show_user.tpl");
        }
        $query = "SELECT current_round FROM #__joomleague WHERE id = $project_id";
        $database->setQuery($query);
        $current_round = $database->loadResult();
        
        $query = "SELECT user_id FROM #__joomleague_tip_members WHERE reminder = 1 AND project_id = $project_id";
        $database->setQuery($query);
        $reminder = $database->loadResultArray();
        $reminderlist = implode(",",$reminder);

        $query = "SELECT name, email FROM #__users WHERE id IN ($reminderlist)";
        $database->setQuery($query);
        $adresses = $database->loadObjectList();

        $mailsubject = str_replace('$DATE$', $thisdate, trim($showUser_config['reminder_subject']));
        $mailsubject = str_replace('$ROUND$', $current_round, $mailsubject);
        $mailsubject = str_replace('$URL$', _JOOMLA_JL_LIVE_SITE, $mailsubject);

        $mailfooter = str_replace('$DATE$', $thisdate, trim($showUser_config['reminder_footer']));
        $mailfooter = str_replace('$ROUND$', $current_round, $mailfooter);
        $mailfooter = str_replace('$URL$', _JOOMLA_JL_LIVE_SITE, $mailfooter);
        $mailfooter = str_replace('$NL$', "\n", $mailfooter);
        $mailfooter = str_replace('$DL$', "\n\n", $mailfooter);        

        $newline="\n";         
 
// Reminder Content - START        
        foreach ($adresses AS $adress) {
        $mailheader = str_replace('$DATE$', $thisdate, trim($showUser_config['reminder_intro']));
        $mailheader = str_replace('$ROUND$', $current_round, $mailheader);
        $mailheader = str_replace('$URL$', _JOOMLA_JL_LIVE_SITE, $mailheader);
        $mailheader = str_replace('$NL$', "\n", $mailheader);
        $mailheader = str_replace('$DL$', "\n\n", $mailheader);
        $mailheader = str_replace('$NAME$', $adress->name, $mailheader);
        mosMail( $mainframe->getCfg( 'mailfrom' ), $mainframe->getCfg( 'fromname' ), $adress->email, $mailsubject, $mailheader.$newline.$mailmessage
                 .$newline.$mailfooter, 0 );
		}
// Reminder Content - END  
       
       	$msg = 'Reminder sent to '. count( $adresses ) .' users';
	      mosRedirect( JL_ADMIN_COMPONENT_LINK.'&act=prediction_users&adminmode='.JL_ADMIN_MODE, $msg );
    }
      
      /**
       * approve prediction user
       *
       * @param array $id
       * @param int(1) $dowhat
       */
      function jl_pred_user_approve ($id, $dowhat) {
        global $database;
        $member = new TipMember($database);
        $member->approve($id, $_POST, $dowhat);
        mosRedirect(JL_ADMIN_COMPONENT_LINK.'&act=prediction_users&adminmode='.JL_ADMIN_MODE);
      }
      
      /**
       * deletes prediction users
       *
       * @param array $id
       */
      function jl_pred_user_remove($id) {
        global $database;
        $member = new TipMember($database);
		      if (!$_POST['project_id']) {
            mosRedirect(JL_ADMIN_COMPONENT_LINK.'&act=prediction_users&adminmode='.JL_ADMIN_MODE, "No Project selected");
		      } else {
            $member->deleteUserTips($id, $_POST, 0);
            mosRedirect(JL_ADMIN_COMPONENT_LINK.'&act=prediction_users&adminmode='.JL_ADMIN_MODE);
		      }
       }
      
      /**
       * prediction user list output
       *
       * @param integer $project_id
       * @param integer $limit
       * @param integer $limitstart
       */
      function jl_pred_user_list($project_id, $limit, $limitstart) {
        global $database;
        
        $where = array();
        
        if ($project_id > 0) {
		      $where[] = "m.project_id = '$project_id'";
	      }
        
      
        $projects[] = mosHTML::makeOption( '0', _JL_ADMIN_SELECT_PROJECT );      
        
        $query = "SELECT p.id as value, p.name as text 
                    FROM #__joomleague AS p
                    LEFT JOIN #__joomleague_predictiongame_project AS pp ON p.id = pp.project_id
                    LEFT JOIN #__joomleague_predictiongame AS pred ON pred.id = pp.prediction_id
                   WHERE p.published = 1 AND pred.published = 1";
        $database->setQuery($query);
        if ($results = $database->loadObjectList()) $projects = array_merge( $projects, $results );
        
        $projectlist = mosHTML::selectList( $projects, 'project_id', 'class="inputbox" onchange="document.adminForm.submit();" size="1"','value', 'text', $project_id );
        unset( $projects );
        unset( $results );
        
        $database->setQuery( "SELECT count(*) FROM #__joomleague_tip_members AS m"
		      . (count( $where ) ? "\nWHERE " . implode( ' AND ', $where ) : "")
	      );
	      $total = $database->loadResult();
        
        require_once( _JOOMLA_JL_PATH . '/administrator/includes/pageNavigation.php' );
	      $pageNav = new mosPageNav( $total, $limitstart, $limit  );

	      $database->setQuery( "SELECT m.*, p.name AS project, u.username, u.name"
		      . "\nFROM #__joomleague_tip_members AS m"
		      . "\nLEFT JOIN #__joomleague_predictiongame_project AS pp ON pp.project_id = m.project_id"
		      . "\nLEFT JOIN #__joomleague_predictiongame AS p ON p.id = pp.prediction_id"
		      . "\nLEFT JOIN #__users AS u ON u.id = m.user_id"
		      . (count( $where ) ? "\nWHERE " . implode( ' AND ', $where ) : "")
		      . "\nORDER BY m.project_id, u.username"
		      . "\nLIMIT $pageNav->limitstart,$pageNav->limit"
	      );

	      $rows = $database->loadObjectList();
       
        $view = JL_ADMIN_TEMPLATE_PATH.'/edit_list_tipmembers.tpl.php';    
        require JL_ADMIN_TEMPLATE_PATH.'/view.tpl.php';

    }
?>